﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Colorful.CMS.Core.Controllers
{
    [Route("Monitor")]
    public class MonitorController : Admin.AdminBaseController
    {
        private readonly IActionDescriptorCollectionProvider _provider;

        public MonitorController(IActionDescriptorCollectionProvider provider)
        {
            _provider = provider;
        }

        [Route("routes")]
        public IActionResult GetRoutes()
        {
            var routes = _provider.ActionDescriptors.Items.Where(a => a.AttributeRouteInfo != null).Select(x => new
            {
                Url = x.AttributeRouteInfo.Template,
                Controller = x.RouteValues["Controller"],
                Action = x.RouteValues["Action"]

            }).OrderBy(a => a.Url).ToList();
            var html = new StringBuilder("<table class=\"am-table\" style=\"margin:30px\">");
            html.Append("<thead><tr><td>Url</td><td>Controller</td><td>Action</td></tr></thead>");
            html.Append("<tbody>");
            foreach (var item in routes)
            {
                html.Append($"<tr><td>{item.Url}</td><td>{item.Controller}</td><td>{item.Action}</td></tr>");
            }
            html.Append("</tbody></table>");
            return this.GetTextResult(html.ToString());
        }
    }
}
